/**
 * 自定义Mutations
 * @module store/mutations
 *
 * @example 定义mutation方式
 *
 * methodName(state, payload) {
 *    state.moduleName.model = payload
 * }
 *
 */
export default {
  /**
   * 设置组件属性库
   * @param state
   * @param props
   */
  setProps(state, props) {
    state.props = props
  },

  /**
   * 设置表单配置实体
   * @param state
   * @param payload
   */
  setFormModel(state, payload) {
    state.formModel = payload
  },
  /**
   * 初始化表单字段
   * @param state
   * @param payload
   */
  setFields(state, payload) {
    state.fields = payload
    state.tab_id = 0;
    console.log(state,'初始化表单')
  },

  /**
   * 设计区新增表单项组件
   * @param state
   * @param field
   */
  addField(state, {field, index,tab=0}) {
    if (field) {
      state.tab_id = tab;
      console.log(field,tab,state.fields)
      state.fields[tab].content.splice(index, 0, field)
    }
  },
  /**
   * 设计区删除表单项组件
   * @param state
   * @param field
   */
  removeField(state, field,tab_id=0) {
    state.fields[state.tab_id].content = state.fields[state.tab_id].content.filter(item => field !== item)
  
  },
  /**
   * 设计区更新表单项组件
   * @param state
   * @param field
   */
  updateField(state, field) {
    state.tab_id = field.tab;
    const index = state.fields[field.tab].content.findIndex(item => item.prop === state.editField.prop)
    state.fields[field.tab].content.splice(index, 1, field)
    state.editField = field
  },
  /**
   * 设计区表单项排序
   * @param state
   * @param newIndex
   * @param oldIndex
   */
  sortFields(state, {newIndex, oldIndex,tab=0}) {
    console.log(111,newIndex, oldIndex,tab);
    const item = state.fields[tab].content[oldIndex]
    const newItem = state.fields[tab].content[newIndex]
    if (!item || !newItem) return
    if (newIndex !== oldIndex) {
      state.fields[tab].content.splice(oldIndex, 1)
      state.fields[tab].content.splice(newIndex, 0, item)
    }

  },
  /**
   * 设置当前正在编辑的表单项组件
   * @param state
   * @param field
   */
  setEditField(state, field) {
    console.log(field,11111111)
    state.editField = field
  },

//TABLE设置
/**
   * 设置组件属性库
   * @param state
   * @param props
   */
  setTableProps(state, props) {
    console.log(props)
    state.tableProps = props
  },

  /**
   * 初始化表单字段
   * @param state
   * @param payload
   */
  setTableFields(state, payload) {
    state.tableField = payload
  },

  addTableField(state, {field, index}) {
    console.log('addTableField',field,index)
    if (field) {
      state.tableField.splice(index, 0, field)
      var obj = {};
      if(state.tableDataField.length){
        obj = state.tableDataField[0];
      }

      if(field.value) {
        obj[field.prop] = field.value
      }else{
        obj[field.prop] = ''
      }
      state.tableDataField[0] = obj;

      console.log('addTableField++++++++++++++',state.tableField,state.tableDataField)
      //
      // state.fields.splice(index, 0, field)
    }
  },
  removeTableField(state, field) {
    console.log(field,state.tableField)
    state.tableField = state.tableField.filter(item => field !== item)
  },
   /**
   * 设置当前正在编辑的表单项组件
   * @param state
   * @param field
   */
  setEditTableField(state, field) {
    console.log(field,'+++++++++++++++++++++');
    state.tableEditField = field
  },

  updateTableField(state, field) {
    const index = state.tableField.findIndex(item => item.prop === state.tableEditField.prop)
    state.tableField.splice(index, 1, field)
    console.log(state.tableField,field)
    state.tableEditField = state.tableField[index]
  },

    /**
   * TABLE项排序
   * @param state
   * @param newIndex
   * @param oldIndex
   */
  sortTableFields(state, {newIndex, oldIndex}) {
    console.log(111,newIndex, oldIndex);
    const item = state.tableField[oldIndex]
    const newItem = state.tableField[newIndex]
    if (!item || !newItem) return
    if (newIndex !== oldIndex) {
      state.tableField.splice(oldIndex, 1)
      state.tableField.splice(newIndex, 0, item)
    }

  },


  //TAB设置
  addTabsField(state, {field, index}) {
    console.log('addTabsField',field,index)
    if (field) {
      // state.tabsField.splice(index, 0, field)
      state.fields.splice(index, 0, field)
      console.log(state.fields,'++++++++++++++++++++++++++++++++++++++++++++++++')
    }
  },
  removeTabsField(state, field) {
    console.log(field)
    // state.tabsField = state.tabsField.filter(item => field !== item)
    state.fields = state.fields.filter(item => field !== item)
  },
   /**
   * 设置当前正在编辑的表单项组件
   * @param state
   * @param field
   */
  setEditTabsField(state, field) {
    console.log(field,'+++++++++++++++++++++');
    state.tabsEditField = field
    state.tab_id = field.id;
  },

  updateTabsField(state, field) {
    console.log(state.fields),field;
    // const index = state.tabsField.findIndex(item => item.title === state.tabsEditField.title)
    const index = state.fields.findIndex(item => item.id === state.tabsEditField.id)
    // state.tabsField.splice(index, 1, field)
    state.fields.splice(index, 1, field)
    state.tabsEditField = field
  },

  initComponentList (state, node) {
    state.componentList = node || {}
    var componentMap = {}
    // 对组件列表扁平化
    for (const key in node) {
      const element = node[key]
      element.child.forEach(value => {
        if (componentMap[value.name]) {
          console.error('存在组件名字相同的组件', value.name)
        } else {
          componentMap[value.name] = value
        }
      })
    }
    state.componentMap = componentMap
  },
  SettingChange (state, node) {
    Object.assign(state.setting, node)
    window.localStorage.setItem('EditorSetting', JSON.stringify(state.setting))
  },


  addSubFromField(state,field){
    state.subFromField = field;
  },
  SET_CONFIG (state, cfg) {
    state.Config = cfg
  },

  SET_USER (state, user) {
    state.app.userInfo = user
  },

  SET_PAGE_INFO (state, info) {
    state.pageInfo = info
  },

  SET_NODE_INFO (state, info) {
    console.log(info,"infosfdsafdsa")
    state.nodeInfo = info
  },

  DATA_HUB_SET (state, info) {
    state.DataHub = info
  },

  SET_CURRENT_LAYOUT(state, payload) {
    console.log(payload,999999)
    state.viewOption.currentLayout = payload
  },

  SET_PAGE_TYPE (state, payload) {
    state.viewOption.pageType = payload
  },

  setEditType (state, payload) {
    state.editType = payload
  },

  //添加按钮相关
  addButtonListField(state, {field, index,tab}){
    console.log('addButtonListField________________',state.tableField,field, index,tab)
    if(field && state.tableField[tab]) {
      state.tableField[tab].children.splice(index, 0, field);
    }
  },

  addLeftButtonField(state, {field, index}){
    console.log('addLeftButtonField',field,index)
    if (field) {
      state.leftButtonField.splice(index, 0, field)
    }
  },


    /**
   * 初始化表单字段
   * @param state
   * @param payload
   */
  setButtonFields(state, payload) {
    if(payload.type=='left'){
      state.leftButtonField = payload.list
    }

    if(payload.type =='right'){
      state.rightButtonField = payload.list
    }
  },


  updateButtonField(state, {field,types}) {
    var index;
    if(types=='left'){
      index = state.leftButtonField.findIndex(item => item.prop === state.ButtonEditField.prop)
      state.leftButtonField.splice(index, 1, field)
    }

    if(types=='right'){
      index = state.rightButtonField.findIndex(item => item.prop === state.ButtonEditField.prop)
      state.rightButtonField.splice(index, 1, field)
    }

    console.log(types=='left' ? state.leftButtonField[index] : state.rightButtonField[index],field)
    state.ButtonEditField = types=='left' ? state.leftButtonField[index] : state.rightButtonField[index]
  },


  sortButtonFields(state, {newIndex, oldIndex,types}) {
    console.log(111,newIndex, oldIndex,types);
    const item = types=='left' ? state.leftButtonField[oldIndex] : state.rightButtonField[oldIndex]
    const newItem = types=='left' ? state.leftButtonField[newIndex] : state.rightButtonField[newIndex]
    if (!item || !newItem) return
    if (newIndex !== oldIndex) {
      types=='left' ? state.leftButtonField.splice(oldIndex, 1) : state.rightButtonField.splice(oldIndex, 1)
      types=='left' ? state.leftButtonField.splice(newIndex, 0, item) : state.rightButtonField.splice(newIndex, 0, item)
    }

  },

  removeLeftButtonField(state, field) {
    console.log(field,state.leftButtonField)
    state.leftButtonField = state.leftButtonField.filter(item => field !== item)
  },



  addRightButtonField(state, {field, index}){
    console.log('addRightButtonField',field,index)
    if (field) {
      state.rightButtonField.splice(index, 0, field)
    }
  },
  removeRightButtonField(state, field) {
    console.log(field,state.rightButtonField)
    state.rightButtonField = state.rightButtonField.filter(item => field !== item)
  },

  setEditButtonField(state, field) {
    console.log(field,'+++++++++++++++++++++');
    state.ButtonEditField = field
  },




}
